/*
 * Package il.ac.biu.cs.grossmm.test.test3
 * File SpyPublished.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.test3;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyFactory.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.DataProcessorBase;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;
import il.ac.biu.cs.grossmm.api.flow.NotificationPoint;
import il.ac.biu.cs.grossmm.api.flow.Notifier;
import il.ac.biu.cs.grossmm.api.flow.Track;
import il.ac.biu.cs.grossmm.api.keys.Key;
import il.ac.biu.cs.grossmm.api.presence.ResourceType;

import java.util.logging.Logger;

public class ProcessorD extends DataProcessorBase implements Notifier<BarPresence>{
	
	static Logger logger = Logger.getLogger("ProcessorD");
	private NotificationPoint<BarPresence> np;

	public ProcessorD()
	{
		super("Processor D");
		logger.info("Created");
	}

	@Override
	public synchronized void run()
	{
		logger.info("Started");
		
		
		np = adm.createNotificationPoint(this, 
			pattern(String.class, 
				mandatory(RESOURCE_TYPE, ResourceType.class), 
				mandatory(CONTENT_TYPE, "application/bar+pidf")), 
			nodeType(BarPresence.class), 1.0);
		
		started();
		
		waitForStop();
		stopped();
		
		logger.info("Stopped");
	}

	public void fetch(Key key, Track track, Root<BarPresence> node, NodeManipulator manipulator)
	{
		// TODO Implement fetch
		// 
		throw new RuntimeException("Not implemented");
	}

	public boolean subscribe(Key key, Track track) throws Exception
	{
		logger.info("key="+key);
		
		Root<BarPresence> barRoot = np.activated(key);
		
		NodeManipulator man = np.writeLock(barRoot);
		man.setValue(barRoot, BarPresence.NAME, (String) key.value());
		man.setValue(barRoot, BarPresence.AVAILABLE, false);
		man.setValue(barRoot, BarPresence.NOTE, "a note by "+key.value());
		np.writeUnlock(barRoot);
		
		return true;
	}

	public void unsubscribe(Key key)
	{
		// TODO Implement unsubscribe
		// 
		throw new RuntimeException("Not implemented");
	}

}
